import router from './router'
import "nprogress/nprogress.css"
import Nprogress from 'nprogress'
import useUserStore from './store/user.js'
import pinia from '@/store'
import { ElMessage } from 'element-plus'
Nprogress.configure({ showSpinner: false })
let userStore = useUserStore(pinia)
// 页面白名单
let whiteList = ["/login",]
// 路由鉴权
router.beforeEach((to, from, next) => {
  
  Nprogress.start();
  let token = userStore.userInfo?.token;
  // 验证通过
  if (token) {
    if (to.path.includes("/manage")) {
      let session = userStore.getUserType()
      if (session && (session == 1 || session == 2)) {
        next()
      }
      else if (session && session == 0) {
        ElMessage({
          type: 'warning',
          message: '无管理权限'
        })
        next(false)
      }
      // userType由当前访问从home页获取
      else {
        ElMessage({
          type: 'warning',
          message: '当前网页未获取密钥'
        })
        next({ path: '/home' })
      }
    }
    else next()
  }
  else {
    // 页面处于白名单
    if (whiteList.includes(to.path)) {
      next()
    }
    // 重定向到登录页
    else {
      next({ path: '/login' })
    }
  }
});

router.afterEach((to, from, next) => {
  Nprogress.done();
})